<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="所属域" prop="domainId">
        <el-select v-model="dataForm.domainId" placeholder="请选择">
          <el-option v-for="(domain, index) in domainList" :label="domain.name" :value="domain.id"
                     :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="策略名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="策略名称"></el-input>
      </el-form-item>
      <el-form-item
        v-for="(domain, index) in dataForm.isomerismControllers"
        :label="'控制器_' + index" :key="index">
        <el-row>
          <el-col :span="4" style="margin-left: 6px">
            <el-input v-model="domain.ip" placeholder="ip:127.0.0.1" :rules="{
          required: true, message: 'ip不能为空', trigger: 'blur'}"></el-input>
          </el-col>
          <el-col :span="4" style="margin-left: 6px">
            <el-input v-model="domain.midlleWare" placeholder="中间件ip" :rules="{
          required: true, message: '中间件ip不能为空', trigger: 'blur'}"></el-input>
          </el-col>
          <el-col :span="3" style="margin-left: 6px">
            <el-input v-model="domain.isoPort" placeholder="端口" :rules="{
          required: true, message: '端口不能为空', trigger: 'blur'}"></el-input>
          </el-col>
          <el-col :span="3" style="margin-left: 6px">
            <el-input v-model="domain.typeName" placeholder="类型" :rules="{
          required: true, message: '类型不能为空', trigger: 'blur'}"></el-input>
          </el-col>
          <el-col :span="2" style="margin-left: 6px">
            <el-button type="danger" v-if="dataForm.isomerismControllers.length>1"
                       @click.prevent="removeDomain(domain)">删除
            </el-button>
          </el-col>
          <el-col :span="2" style="margin-left: 6px">
            <el-button type="primary" v-if="index == dataForm.isomerismControllers.length-1" @click="addDomain">新增
            </el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="策略类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="策略类型">
          <el-option label="可信根" value="1"></el-option>
          <el-option label="业务连续主动变迁" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="策略详情" prop="detail">
        <el-input
          type="textarea"
          :autosize="{ minRows: 20, maxRows: 30}"
          v-model="dataForm.detail"
          placeholder="策略详情"
        ></el-input>
      </el-form-item>
      <el-form-item label="策略描述" prop="policyDesc">
        <el-input v-model="dataForm.policyDesc" placeholder="策略描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,

                dataForm: {
                    id: 0,
                    domainId: "",
                    name: "",
                    type: "",
                    detail: "",
                    desc: "",
                    policyDesc: '',
                    isomerismControllers: [{}],
                },
                domainList: [],
                dataRule: {
                    domainId: [
                        {required: true, message: "请选择所属域", trigger: "blur"}
                    ],
                    name: [
                        {required: true, message: "策略名称不能为空", trigger: "blur"}
                    ],
                    type: [
                        {required: true, message: "策略类型不能为空", trigger: "blur"}
                    ],
                    detail: [
                        {required: true, message: "策略详情不能为空", trigger: "blur"}
                    ],
                    policyDesc: [
                        {required: true, message: "策略描述不能为空", trigger: "blur"}
                    ]
                }
            };
        },
        methods: {
            getDomain() {
                this.domainList = []
                this.$http({
                    url: this.$http.adornPortalUrl("/generator/domain/list"),
                    method: "get",
                    params: this.$http.adornParams({})
                }).then(({data}) => {
                    if (data && data.code === 0) {
                        let list = data.domains;
                        for (let i = 0; i < list.length; i++) {
                            this.domainList.push({
                                id: list[i].domainId,
                                name: list[i].domainName
                            });
                        }
                    }
                });
            },
            removeDomain(item) {
                let index = this.dataForm.isomerismControllers.indexOf(item)
                if (index !== -1) {
                    this.dataForm.isomerismControllers.splice(index, 1)
                }
            },
            addDomain() {
                this.dataForm.isomerismControllers.push({});
            },
            init(id) {
                this.getDomain();
                this.dataForm.id = id || 0;
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs["dataForm"].resetFields();
                    if (this.dataForm.id) {
                        this.$http({
                            url: this.$http.adornEndogenousUrl(
                                `/policyconfig/info/${this.dataForm.id}`
                            ),
                            method: "get",
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm.name = data.policyConfig.name;
                                this.dataForm.domainId = data.policyConfig.domainId;
                                this.dataForm.type = data.policyConfig.type;
                                this.dataForm.detail = data.policyConfig.detail;
                                this.dataForm.policyDesc = data.policyConfig.policyDesc;
                                this.dataForm.isomerismControllers = data.policyConfig.isomerismControllerConfig;
                            }
                        });
                    }
                });
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs["dataForm"].validate(valid => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornEndogenousUrl(
                                `/policyconfig/${!this.dataForm.id ? "save" : "update"}`
                            ),
                            method: "post",
                            data: this.$http.adornData({
                                id: this.dataForm.id || undefined,
                                domainId: this.dataForm.domainId,
                                name: this.dataForm.name,
                                type: this.dataForm.type,
                                detail: this.dataForm.detail,
                                policyDesc: this.dataForm.policyDesc,
                                isomerismControllerConfig: this.dataForm.isomerismControllers
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: "操作成功",
                                    type: "success",
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false;
                                        this.$emit("refreshDataList");
                                    }
                                });
                            } else {
                                this.$message.error(data.msg);
                            }
                        });
                    }
                });
            }
        }
    };
</script>
